package com.zwz.service.impl;

import com.zwz.dto.JudgmentDocumentDTO;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.JudgmentDocument;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.JudgmentDocumentMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.JudgmentDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class JudgmentDocumentServiceImpl implements JudgmentDocumentService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private JudgmentDocumentMapper documentMapper;

    @Override
    public JudgmentDocumentDTO createWithMain(   String enterpriseId) {
        // 创建主表记录
        // 创建主表记录
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(enterpriseId));



        // 设置子表关联
        JudgmentDocument document = new JudgmentDocument();

        int k=0;
        document.setId(k++);
// 嵌入真实数据接入
        document.setDocumentCount(1);


        document.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        // 业务校验
        validateDocument(document);

        // 插入子表
        documentMapper.insert(document);

        return new JudgmentDocumentDTO(enterpriseInfo, document);
    }

    @Override
    public List<JudgmentDocument> getByDateRange(Date start, Date end) {
        return null;
    }

    private RiskInfoMain createMainRecord(Integer companyId) {
        RiskInfoMain main = new RiskInfoMain();
        main.setCompanyId(companyId);
        mainMapper.insert(main);
        return main;
    }

    private void validateDocument(JudgmentDocument document) {
        if (document.getHasJudgment() != null &&
                document.getHasJudgment() &&
                document.getPublishDate() == null) {
            throw new IllegalArgumentException("存在裁判文书必须包含发布日期");
        }
    }
}